package com.zeus.gen.base.service.impl;

import com.zeus.gen.base.dao.MyBatisDao;
import com.zeus.gen.base.dto.Query;
import com.zeus.gen.base.model.BaseModel;
import com.zeus.gen.base.service.BaseService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 实现BaseService抽象类
 *
 * @author
 */
public abstract class BaseServiceImpl<T extends BaseModel> implements BaseService<T> {

    @Resource(name = "myBatisDao")
    protected MyBatisDao<T> dao;

    /**
     * service对应实体字节码
     *
     * @return
     */
    public abstract Class<T> getEntityClass();

    @Override
    public T findObjById(Serializable id) {
        return (T) dao.findObjById(getEntityClass(), id);
    }

    @Override
    public T findObj() {
        Query query = new Query();
        return this.findObj(query);
    }

    @Override
    public T findObj(Query query) {
        return this.findObj(query, null);
    }

    @Override
    public T findObj(Query query, String sqlId) {
        return (T) dao.findObj(this.getEntityClass(), query, sqlId);
    }

    @Override
    public <K, V>  Map<K, V> findMap(Query query, String sqlId, String mapKey) {
        return (Map<K, V>) dao.findMap(this.getEntityClass(), query, sqlId, mapKey);
    }

    @Override
    public List findList() {
        Query query = new Query();
        return this.findList(query);
    }

    @Override
    public List findList(Query query) {
        return this.findList(query, null);
    }

    @Override
    public List findList(Query query, boolean includeCount) {
        return this.findList(query, null, includeCount);
    }

    @Override
    public List findList(Query query, String sqlId) {
        return this.findList(query, sqlId, true);
    }

    @Override
    public List findList(Query query, String sqlId, boolean includeCount) {
        return dao.findList(this.getEntityClass(), query, sqlId, includeCount);
    }

    @Override
    public int count(Query query) {
        return this.count(query, null);
    }

    @Override
    public int count(Query query, String sqlId) {
        return dao.count(this.getEntityClass(), query, sqlId);
    }

    @Override
    public int save(T obj) {
        return this.save(obj, null);
    }

    @Override
    public int save(T obj, String sqlId) {
        return dao.save(obj, sqlId);
    }

    @Override
    public int update(T obj) {
        return this.update(obj, null);
    }

    @Override
    public int update(T obj, String sqlId) {
        return dao.update(obj, sqlId);
    }

    @Override
    public int saveOrUpdate(T obj) {
        if (obj.getId() == null) {
            return this.save(obj);
        } else {
            return this.updateSelective(obj);
        }
    }

    @Override
    public int updateSelective(T obj) {
        return this.updateSelective(obj, null);
    }

    @Override
    public int updateSelective(T obj, String sqlId) {
        return dao.updateSelective(obj, sqlId);
    }

    @Override
    public int batchUpdate(List<? extends Serializable> idList, Object obj) {
        return this.batchUpdate(idList, obj, null);
    }

    @Override
    public int batchUpdate(List<? extends Serializable> idList, Object obj, String sqlId) {
        return dao.batchUpdate(this.getEntityClass(), idList, obj, sqlId);
    }

    @Override
    public int delete(Serializable id) {
        return this.delete(id, null);
    }

    @Override
    public int delete(Serializable id, String sqlId) {
        return dao.delete(this.getEntityClass(), id, sqlId);
    }

    @Override
    public int delete(T obj) {
        return this.delete(obj, null);
    }

    @Override
    public int delete(T obj, String sqlId) {
        return dao.delete(obj, sqlId);
    }

    @Override
    public <E> E findOne(Query query, String sqlId) {
        return (E) dao.findObj(this.getEntityClass(), query, sqlId);
    }
}